package com.hzit.seckill.controller;

import com.hzit.common.utils.MyDate;
import com.hzit.seckill.entity.SeckillGoods;
import com.hzit.seckill.service.ISeckillGoodsService;
import com.hzit.seckill.service.ISeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbxfwf
 * @since 2023-10-12
 */
@Controller
@RequestMapping
public class SeckillGoodsController {
	@Autowired
	private ISeckillGoodsService seckillGoodsService;
	@Autowired
	private ISeckillOrderService orderService;

	/**
	 * 1. 秒杀首页
	 * 思路：
	 *   1.1 通过定时器每次将redis与数据库比对，不一致就放到redis中，第一次肯定全部放到redis中
	 *   1.2 再通过redis查询出当前用户的秒杀商品。
	 */


	@GetMapping({"/","/seckill-index.html"})
	public String index(Model model, HttpSession session){
		//1.1 将当前时间开始的五个时间段查询出来放到model中
		List<Date> dateMenus = MyDate.getDateMenus();
		model.addAttribute("dateMenus",dateMenus);
		//1.2 查询出秒杀商品 (从redis查询出秒杀商品)
		List<SeckillGoods> seckillGoods = seckillGoodsService.getSeckillGoodsFromRedis();
		//1.3 将redis中秒杀商品放到model中
		model.addAttribute("sekillGoodsList",seckillGoods);

		return "seckill-index";
	}

	//2. 根据商品id查询商品
	@GetMapping("/seckill")
	public String findSeckillGoods(Long id,Model model){
		SeckillGoods seckillGoods = seckillGoodsService.getById(id);
		model.addAttribute("seckillGoods",seckillGoods);
		return "seckill-item";
	}

}
